import {
	Request
} from '/js_sdk/qq-request/index.js';

let bestHost = ''
let imagesHost1 = ''
let env
// let imagesHost1 = 'https://ytl.mifanny.com/profile'
// let domain = 'https://ytl.mifanny.com'
let domain = 'https://ytlsc.mifanny.com'
// #ifdef MP-WEIXIN
env = uni.getAccountInfoSync().miniProgram.envVersion;
if (env === "trial") {
	// bestHost = domain + '/staff'
	// imagesHost1 = 'https://ytl.mifanny.com/profile'
	bestHost = 'https://api.yantianling.com/staff/'
	imagesHost1 = 'https://filebucket-1318161244.cos.ap-shanghai.myqcloud.com'
} else if (env === "develop") {
	// bestHost = domain + '/staff'
	// imagesHost1 = 'https://ytl.mifanny.com/profile'
	bestHost = 'https://api.yantianling.com/staff/'
	imagesHost1 = 'https://filebucket-1318161244.cos.ap-shanghai.myqcloud.com'
} else {
	// bestHost = domain + '/staff'
	// imagesHost1 = 'https://ytl.mifanny.com/profile'
	// 测试环境
	// bestHost = 'https://ytlsc.mifanny.com'
	// imagesHost1 = 'https://ytl.mifanny.com/profile'
	// ytl环境
	bestHost = 'https://api.yantianling.com/staff/'
	imagesHost1 = 'https://filebucket-1318161244.cos.ap-shanghai.myqcloud.com'
}
// #endif

const request = new Request({
	host: bestHost,
	showLoading: true,
	header: {
		"Authorization": 'Bearer ' + uni.getStorageSync('token')
	},
	before(config) {
		config.header['Authorization'] = 'Bearer ' + uni.getStorageSync('token');
		return config;
	},
	// ... 其他参数参考参数说明
	after(requestTaskPromise) {
		return requestTaskPromise
			.then(ret => ret)
			.catch(console.log);
	},
	// 自定义返回数据
	briefReturn(jsonResponse) {
		// console.log('jsonResponse: ',jsonResponse);
		// if(jsonResponse.response.data.code === 200){
		// 	return jsonResponse.response.data.msg
		// }else{
		// 	return jsonResponse.response.data
		// }
		return jsonResponse.response.data;
	},
	// 校验器
	validator(response) {
		// throw new Error(`请求异常[${response.getStatusCode()}]:${response.getError()}`);
		// console.log('response: ', response);
		// console.log('response.getStatusCode(): ', response.getStatusCode());
		if (response.getStatusCode() == 200) {
			// return response
			if (response.response?.data?.code == 401) {
				uni.showModal({
					// title: '提示',
					title: '请先登录',
					// content: "您的登录已过期，请重新登录！",
					confirmText: "去登录",
					cancelText: "取消",
					confirmColor: "#18bc37",
					success(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/login/login'
							});
							uni.removeStorageSync('userinfo')
							uni.setStorageSync('token', '');
							uni.$emit('loginStatusChange',{login:'out'});
						}
						if (res.cancel) {
							// 清除缓存
							uni.removeStorageSync('userinfo')
							uni.setStorageSync('token', '');
						}
					}
				});
			}
		} else if (response.getStatusCode() == 404) {
			uni.showModal({
				title: '提示',
				content: "服务器错误",
				showCancel: false
			});
		} else if (response.getStatusCode() == 424) {
			uni.showModal({
				title: '请先登录',
				// content: "您的登录已过期，请重新登录！",
				confirmText: "去登录",
				cancelText: "取消",
				success: (res) => {
					if (res.confirm) {
						uni.clearStorageSync();
						// uni.reLaunch({
						// 	url: '/pages/login/login'
						// });
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}
				}
			});
		} else if (response.getStatusCode() == 500) {
			uni.showModal({
				title: "提示",
				content: "服务器错误，请稍后重试~",
				showCancel: false
			})
		} else if (response.getStatusCode() == 502) {
			uni.showModal({
				title: "提示",
				content: "服务器错误~",
				showCancel: false
			})
		} else {
			uni.showModal({
				title: "提示",
				content: "未知错误~",
				showCancel: false
			})
		}
	}
});

export {
	request,
	bestHost
}